home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0027_Prime Numbers.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  723b  |  30 lines

  1. {
  2. MICHAEL M. BYRNE
  3.  
  4. > the way, it took about 20 mins. on my 386/40 to get prime numbers
  5. > through  20000. I tried to come up With code to do the same With
  6. > Turbo but it continues to elude me. Could anybody explain
  7. > how to Write such a routine in Pascal?
  8.  
  9. Here is a simple Boolean Function For you to work With.
  10. }
  11.  
  12. Function Prime(N : Integer) : Boolean;
  13. {Returns True if N is a prime; otherwise returns False. Precondition: N > 0.}
  14. Var
  15.   I : Integer;
  16. begin
  17.   if N = 1 then
  18.     Prime := False
  19.   else
  20.   if N = 2 then
  21.     Prime := True
  22.   else
  23.   begin { N > 2 }
  24.     Prime := True; {tentatively}
  25.     For I := 2 to N - 1 do
  26.       if (N mod I = 0) then
  27.         Prime := False;
  28.   end; { N > 2 }
  29. end;
  30.